#ifndef __COMM_HPP__
#define __COMM_HPP__

#include<iostream>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<string>
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<string.h>
#include<stdlib.h>
#include<error.h>
#include"log.hpp"

using namespace std;

//用这两个指定key的生成
const string pathname="/home/fx";
const int proj_id =0x6666;
#define Ssize 4096// 一般以4K为标准 单位字节
Log log;

//生成key
key_t GetKey()
{
    key_t k = ftok(pathname.c_str(),proj_id);
    if(k < 0)//获取失败
    {
        log(Fatal,"ftok error:%s ",strerror(errno));
        exit(1);
    }
    log(Info,"ftok success, key is:%d ",k);
    return k;
}
//获取共享内存
int _GetShareMem(int flag)
{
    key_t k = GetKey();
    int shmid = shmget(k,Ssize,flag);
    if(shmid < 0)
    {
        log(Fatal,"shmget error:%s ",strerror(errno));
        exit(2);
    }
    log(Info,"creat share memory success, shmid is:%d ",shmid);
    return shmid;
}

int GetShareMem()
{
    int flag = IPC_CREAT;
    return _GetShareMem(flag);
}

int CreatShareMem()
{
    int flag = IPC_CREAT|IPC_EXCL|0666;
    return _GetShareMem(flag);
}




#define FIFO_FILE "./myfifo"  // 查????
#define MODE 0664

enum{
    FIFO_CREAT_ERR = 1,
    FIFO_DELETE_ERR = 2,
    FIFO_OPEN_ERR = 3
};

class Init
{
public:
    Init()
    {
        int n = mkfifo(FIFO_FILE,MODE);
        if(n==-1)
        {
            perror("mkfifo");
            exit(FIFO_CREAT_ERR);
        }
    }
    ~Init()
    {
    
        int m = unlink(FIFO_FILE);
        if(m == -1)//删除失败
        {
            perror("unlink");
            exit(FIFO_DELETE_ERR);
        }
    }
};



#endif